﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dycox.Collections;

namespace Dycox.Workflows
{
    public class TransitionOperationCollection : OwnedCollection<TransitionOperation, Transition>
    {
        public TransitionOperationCollection(Transition transition)
            : base(transition)
        {

        }

        protected override void SetItemOwner(TransitionOperation item, Transition parent)
        {
            if (parent != null)
            {
                if ((item.SupportedTransitionTypes & parent.Type) != parent.Type)
                    throw new WorkflowException("Operation " + item.GetType() + " is not supported on " + parent.Type);

                if (item.Exclusive && this.Contains(item, TransitionOperationComparer.Instance))
                    throw new WorkflowException("Operation " + item.GetType() + " cannot add to this collection any more.");
            }

            item.Transition = parent;
        }

        protected override Transition GetItemOwner(TransitionOperation item)
        {
            return item.Transition;
        }
    }
}
